package com.yinhe.module.cjl;
import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhContract;
import com.yinhe.system.service.ISysUserService;
import com.yinhe.system.service.IYhContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/module/cjl/contract")
public class CjlContractController extends BaseController{
    @Autowired
    private IYhContractService yhContractService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询合同台账列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhContract yhContract)
    {
        startPage();
        List<YhContract> list = yhContractService.selectYhContractList(yhContract);
        return getDataTable(list);
    }
    /**
     * 查询合同台账列表,用于资料查询
     */
    @GetMapping("/searchlist")
    public TableDataInfo searchlist(YhContract yhContract)
    {
        List<YhContract> list = yhContractService.selectYhContractSearchList(yhContract);
        return getDataTable(list);
    }
    /**
     * 查询用户名查询
     */
    @GetMapping("/userlist")
    public TableDataInfo userlist(SysUser user) {
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }
    /**
     * 导入合同台账列表
     */
    @Log(title = "合同台账", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('module:cjl:contract:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<YhContract> util = new ExcelUtil<YhContract>(YhContract.class);
        List<YhContract> contractList = util.importExcel(file.getInputStream());
        String message = yhContractService.importContract(contractList, updateSupport);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<YhContract> util = new ExcelUtil<YhContract>(YhContract.class);
        util.importTemplateExcel(response, "监管任务数据");
    }
    /**
     * 查询监管任务概览列表
     */
    @RequestMapping("/sequenceNumber/{sequenceNumber}")
    public TableDataInfo list(@PathVariable String sequenceNumber){
        List<YhContract> Number = yhContractService.selectYhContractSequenceNumber(sequenceNumber);
        return getDataTable(Number);
    }


    /**
     * 导出合同台账列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:contract:export')")
    @Log(title = "合同台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhContract yhContract)
    {
        List<YhContract> list = yhContractService.selectYhContractList(yhContract);
        ExcelUtil<YhContract> util = new ExcelUtil<YhContract>(YhContract.class);
        util.exportExcel(response, list, "监管任务数据");
    }


    /**
     * 获取合同台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhContractService.selectYhContractById(id));
    }

    /**
     * 新增合同台账
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:contract:add')")
    @Log(title = "合同台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhContract yhContract)
    {
        yhContract.setContractAssociationStatus("1");
        yhContract.setContractAssociationNo(yhContract.getSequenceNumber()+yhContract.getQuantity()+yhContract.getEquipVersion());
        long represent = Long.parseLong(yhContract.getRepresentativeOffice());
        yhContract.setDeptId(represent);
        return toAjax(yhContractService.insertYhContract(yhContract));
    }

    /**
     * 修改合同台账
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:contract:edit')")
    @Log(title = "合同台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhContract yhContract)
    {
        yhContract.setContractAssociationNo(yhContract.getSequenceNumber()+yhContract.getQuantity()+yhContract.getEquipVersion());
        long represent = Long.parseLong(yhContract.getRepresentativeOffice());
        yhContract.setDeptId(represent);
        return toAjax(yhContractService.updateYhContract(yhContract));
    }

    /**
     * 删除合同台账
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:contract:remove')")
    @Log(title = "合同台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhContractService.deleteYhContractByIds(ids));
    }
}
